package sjsu.android;

public class ChangeDate {
	public final int month;
	public final int day;
	public final int year;
	int[] DAYS = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	public ChangeDate(int m, int d, int y) {
	if (!increment(m, d, y))
	throw new RuntimeException("Invalid");
	month = m;
	day = d;
	year = y;
	}
	private static boolean increment(int m, int d, int y) {
	int[] DAYS = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	if (m < 1 || m > 12)
	return false;
	if (d < 1 || d > DAYS[m])
	return false;
	if (m == 2 && d == 29 && !isLeapYear(y))
	return false;
	return true;
	}
	private static boolean isLeapYear(int y) {
	if (y % 400 == 0)
	return true;
	if (y % 100 == 0)
	return false;
	return (y % 4 == 0);
	}
	public ChangeDate next() {
	if (increment(month, day + 1, year))
	return new ChangeDate(month, day + 1, year);
	else if (increment(month + 1, 1, year))
	return new ChangeDate(month + 1, 1, year);
	else
	return new ChangeDate(1, 1, year + 1);
	}
	public ChangeDate previous() {
		int currentdays = 0;
		if (increment(month, day -1, year)){
		return new ChangeDate(month, day - 1, year);
		}else if (increment(month - 1, 1, year)){
			currentdays = getMonth(month-1);
		return new ChangeDate(month - 1, currentdays, year);
		}else
		return new ChangeDate(12, 31, year - 1);
		}
	public int getMonth(int currentMonth) {
		int days = DAYS[currentMonth];
		return days;
	}
	public int getDay() {
		return day;
	}
	public int getYear() {
		return year;
	}
	public String toString() {
	return day + "-" + month + "-" + year;
	}
}
